package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.masterdata.starter.service.IMerchantService;
import com.jeomo.mem.starter.entity.MemberConsumeDetail;
import com.jeomo.mem.starter.mapper.MemConsumeDetailMapper;
import com.jeomo.mem.starter.service.IMemConsumeDetailService;
import com.jeomo.mem.starter.service.IMemPointsSrcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员消费明细服务
 *
 * @author:Jackpot
 * @create 2020-10-30 15:42
 * @description:
 */
@Service
@Transactional
public class MemConsumeDetailServiceImpl extends BaseServiceImpl<MemConsumeDetailMapper, MemberConsumeDetail>
    implements IMemConsumeDetailService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String CARD_NO = "card_no";
    private final String PAY_TIME = "pay_time";
    private final String TIME_FROM = "time_from";
    private final String TIME_TO = "time_to";

    @Resource
    private IMerchantService merchantService;
    @Autowired
    private IMemPointsSrcService memPointsSrcService;

    @Override
    public PageResponseDto pageSearch(PageQueryDto dto) {

        Map<String, Object> searchParams = dto.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_EQ+CARD_NO,searchParams.get(CARD_NO));
            params.put(SearchParam.SEARCH_GE+PAY_TIME,searchParams.get(TIME_FROM));
            params.put(SearchParam.SEARCH_LE+PAY_TIME,searchParams.get(TIME_TO));
            dto.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(PAY_TIME, Sort.DESC);
        dto.setSorts(sort);

        PageResponseDto response = super.pageSearch(dto);
        Map<Object, Object> cache = memPointsSrcService.queryCache();
        Map<String, String> merchantMap = merchantService.queryCache();
        //2. 处理返回结果
        List<MemberConsumeDetail> items = response.getItems();
        for (MemberConsumeDetail item : items) {
            item.setSrcTypeName(cache.get(item.getSrcType()).toString());
            item.setMerchantName(merchantMap.get(item.getMerchant()));
        }
        return response;
    }

}
